home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 7 / PC World Interactive 7.iso / program / pastutor.EXE / tutor.pas next >
Pascal/Delphi Source File  |  1998-04-02  |  9KB  |  350 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision 2.0 Demo                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program Tutorial;
  9.  
  10. uses Drivers, Memory, TutConst, Objects, Views, Menus, App, Editors, 
  11.   Orders, Stocks, StdDlg, TutTypes, Count, Dialogs, Supplier, Validate;
  12.  
  13. var
  14.   ResFile: TResourceFile;
  15.  
  16. type
  17.   TTutorApp = object(TApplication)
  18.     ClipboardWindow: PEditWindow;
  19.     OrderWindow: POrderDialog;
  20.     constructor Init;
  21.     destructor Done; virtual;
  22.     procedure CancelOrder;
  23.     procedure DoAboutBox;
  24.     procedure EnterNewOrder;
  25.     procedure HandleEvent(var Event: TEvent); virtual;
  26.     procedure InitMenuBar; virtual;
  27.     procedure InitStatusLine; virtual;
  28.     procedure LoadDesktop;
  29.     procedure NewWindow;
  30.     procedure OpenOrderWindow;
  31.     procedure OpenWindow;
  32.     procedure SaveDesktop;
  33.     procedure SaveOrderData;
  34.     procedure ShowOrder(AOrderNum: Integer);
  35.   end;
  36.  
  37. procedure TutorStreamError(var S: TStream); far;
  38. var
  39.   ErrorMessage: String;
  40. begin
  41.   case S.Status of
  42.     stError: ErrorMessage := 'Stream access error';
  43.     stInitError: ErrorMessage := 'Cannot initialize stream';
  44.     stReadError: ErrorMessage := 'Read beyond end of stream';
  45.     stWriteError: ErrorMessage := 'Cannot expand stream';
  46.     stGetError: ErrorMessage := 'Unregistered type read from stream';
  47.     stPutError: ErrorMessage := 'Unregistered type written to stream';
  48.     end;
  49.   DoneVideo;
  50.   PrintStr('Error: ' + ErrorMessage);
  51.   Halt(Abs(S.Status));
  52. end;
  53.  
  54. constructor TTutorApp.Init;
  55. var
  56.   R: TRect;
  57. begin
  58.   MaxHeapSize := 8192;
  59.   EditorDialog := StdEditorDialog;
  60.   StreamError := @TutorStreamError;
  61.   RegisterObjects;
  62.   RegisterViews;
  63.   RegisterMenus;
  64.   RegisterEditors;
  65.   RegisterApp;
  66.   RegisterDialogs;
  67.   RegisterValidate;
  68.   RegisterGlobals;
  69.   RegisterOrders;
  70.   RegisterCount;
  71.   RegisterStocks;
  72.   ResFile.Init(New(PBufStream, Init('TUTORIAL.TVR', stOpen, 1024)));
  73.   inherited Init;
  74.   Desktop^.GetExtent(R);
  75.   ClipBoardWindow := New(PEditWindow, Init(R, '', wnNoNumber));
  76.   if Application^.ValidView(ClipboardWindow) <> nil then
  77.   begin
  78.     ClipboardWindow^.Hide;
  79.     InsertWindow(ClipboardWindow);
  80.     Clipboard := ClipboardWindow^.Editor;
  81.     Clipboard^.CanUndo := False;
  82.   end;
  83.   LoadOrders;
  84.   CurrentOrder := 0;
  85.   OrderInfo := POrderObj(OrderColl^.At(CurrentOrder))^.TransferRecord;
  86.   LoadStock;
  87.   LoadSuppliers;
  88.   DisableCommands([cmOrderNext, cmOrderPrev, cmOrderCancel, cmOrderSave]);
  89. end;
  90.  
  91. destructor TTutorApp.Done;
  92. begin
  93.   if Assigned(OrderColl) then Dispose(OrderColl, Done);
  94.   inherited Done;
  95.   ResFile.Done;
  96. end;
  97.  
  98. procedure TTutorApp.CancelOrder;
  99. begin
  100.   if CurrentOrder < OrderColl^.Count then
  101.     ShowOrder(CurrentOrder)
  102.   else
  103.   begin
  104.     Dispose(TempOrder, Done);
  105.     ShowOrder(CurrentOrder - 1);
  106.   end;
  107.   EnableCommands([cmOrderNew]);
  108. end;
  109.  
  110. procedure TTutorApp.DoAboutBox;
  111. begin
  112.   ExecuteDialog(PDialog(ResFile.Get('ABOUTBOX')), nil);
  113. end;
  114.  
  115. procedure TTutorApp.EnterNewOrder;
  116. begin
  117.   OpenOrderWindow;
  118.   CurrentOrder := OrderColl^.Count;
  119.   TempOrder := New(POrderObj, Init);
  120.   OrderInfo := TempOrder^.TransferRecord;
  121.   with OrderWindow^ do
  122.   begin
  123.     SetData(OrderInfo);
  124.     Counter^.SetCurrent(CurrentOrder + 1);
  125.   end;
  126.   DisableCommands([cmOrderNext, cmOrderPrev, cmOrderNew]);
  127.   EnableCommands([cmOrderCancel, cmOrderSave]);
  128. end;
  129.  
  130. procedure TTutorApp.HandleEvent(var Event: TEvent);
  131. var
  132.   R: TRect;
  133. begin
  134.   inherited HandleEvent(Event);
  135.   if Event.What = evCommand then
  136.   begin
  137.     case Event.Command of
  138.       cmOpen:
  139.         begin
  140.           OpenWindow;
  141.           ClearEvent(Event);
  142.         end;
  143.       cmNew:
  144.         begin
  145.           NewWindow;
  146.           ClearEvent(Event);
  147.         end;
  148.       cmClipShow:
  149.         with ClipBoardWindow^ do
  150.         begin
  151.           Select;
  152.           Show;
  153.           ClearEvent(Event);
  154.         end;
  155.       cmAbout:
  156.         begin
  157.           DoAboutBox;
  158.           ClearEvent(Event);
  159.         end;
  160.  
  161.       cmOrderWin:
  162.         begin
  163.           OpenOrderWindow;
  164.           ClearEvent(Event);
  165.         end;
  166.       cmStockWin:
  167.         begin
  168.           ExecuteDialog(New(PStockDialog, Init), StockInfo);
  169.           ClearEvent(Event);
  170.         end;
  171.       cmSupplierWin:
  172.         begin
  173.           ExecuteDialog(New(PSupplierDialog, Init), SupplierInfo);
  174.           ClearEvent(Event);
  175.         end;
  176.  
  177.       cmOrderNew:
  178.         begin
  179.           EnterNewOrder;
  180.           ClearEvent(Event);
  181.         end;
  182.       cmOrderSave:
  183.         begin
  184.           SaveOrderData;
  185.           ClearEvent(Event);
  186.         end;
  187.       cmOrderCancel:
  188.         begin
  189.           CancelOrder;
  190.           ClearEvent(Event);
  191.         end;
  192.       cmOrderNext:
  193.         begin
  194.           ShowOrder(CurrentOrder + 1);
  195.           ClearEvent(Event);
  196.         end;
  197.       cmOrderPrev:
  198.         begin
  199.           ShowOrder(CurrentOrder - 1);
  200.           ClearEvent(Event);
  201.         end;
  202.  
  203.       cmOptionsVideo:
  204.         begin
  205.           SetScreenMode(ScreenMode xor smFont8x8);
  206.           ClearEvent(Event);
  207.         end;
  208.       cmOptionsSave:
  209.         begin
  210.           SaveDesktop;
  211.           ClearEvent(Event);
  212.         end;
  213.       cmOptionsLoad:
  214.         begin
  215.          LoadDesktop;
  216.          ClearEvent(Event);
  217.         end;
  218.     end;
  219.   end;
  220. end;
  221.  
  222. procedure TTutorApp.InitMenuBar;
  223. begin
  224.   MenuBar := PMenuBar(ResFile.Get('MAINMENU'));
  225. end;
  226.  
  227. procedure TTutorApp.InitStatusLine;
  228. var
  229.   R: TRect;
  230. begin
  231.   StatusLine := PStatusLine(ResFile.Get('STATUS'));
  232.   GetExtent(R);
  233.   StatusLine^.MoveTo(0, R.B.Y - 1);
  234. end;
  235.  
  236. procedure TTutorApp.LoadDesktop;
  237. var
  238.   DesktopFile: TBufStream;
  239.   TempDesktop: PDesktop;
  240.   R: TRect;
  241. begin
  242.   DesktopFile.Init('DESKTOP.TUT', stOpenRead, 1024);
  243.   TempDesktop := PDesktop(DesktopFile.Get);
  244.   DesktopFile.Done;
  245.   if ValidView(TempDesktop) <> nil then
  246.   begin
  247.     Desktop^.Delete(ClipboardWindow);
  248.     Delete(Desktop);
  249.     Dispose(Desktop, Done);
  250.     Desktop := TempDesktop;
  251.     Insert(Desktop);
  252.     GetExtent(R);
  253.     R.Grow(0, -1);
  254.     Desktop^.Locate(R);
  255.     InsertWindow(ClipboardWindow);
  256.   end;
  257. end;
  258.  
  259. procedure TTutorApp.NewWindow;
  260. var
  261.   R: TRect;
  262. begin
  263.   R.Assign(0, 0, 75, 20);
  264.   InsertWindow(New(PEditWindow, Init(R, '', wnNoNumber)));
  265. end;
  266.  
  267. procedure TTutorApp.OpenOrderWindow;
  268. var
  269.   R: TRect;
  270. begin
  271.   if Message(Desktop, evBroadcast, cmFindOrderWindow, nil) = nil then
  272.   begin
  273.     OrderWindow := New(POrderDialog, Init);
  274.     InsertWindow(OrderWindow);
  275.   end
  276.   else
  277.     if PView(OrderWindow) <> Desktop^.TopView then OrderWindow^.Select;
  278.   ShowOrder(CurrentOrder);
  279. end;
  280.  
  281. procedure TTutorApp.OpenWindow;
  282. var
  283.   R: TRect;
  284.   FileDialog: PFileDialog;
  285.   TheFile: FNameStr;
  286. const
  287.   FDOptions: Word = fdOKButton + fdOpenButton;
  288. begin
  289.   TheFile := '*.*';
  290.   FileDialog := New(PFileDialog, Init(TheFile, 'Open file', '~F~ile name',
  291.     FDOptions, 1));
  292.   if ExecuteDialog(FileDialog, @TheFile) <> cmCancel then
  293.   begin
  294.     R.Assign(0, 0, 75, 20);
  295.     InsertWindow(New(PEditWindow, Init(R, TheFile, 0)));
  296.   end;
  297. end;
  298.  
  299. procedure TTutorApp.SaveDesktop;
  300. var
  301.   DesktopFile: TBufStream;
  302. begin
  303.   Desktop^.Delete(ClipboardWindow);
  304.   DesktopFile.Init('DESKTOP.TUT', stCreate, 1024);
  305.   DesktopFile.Put(Desktop);
  306.   DesktopFile.Done;
  307.   InsertWindow(ClipboardWindow);
  308. end;
  309.  
  310. procedure TTutorApp.SaveOrderData;
  311. begin
  312.   if OrderWindow^.Valid(cmClose) then
  313.   begin
  314.     OrderWindow^.GetData(OrderInfo);
  315.     if CurrentOrder = OrderColl^.Count then
  316.     begin
  317.       OrderColl^.Insert(TempOrder);
  318.       OrderWindow^.Counter^.IncCount;
  319.     end
  320.     else POrderObj(OrderColl^.At(CurrentOrder))^.TransferRecord := OrderInfo;
  321.     SaveOrders;
  322.     ShowOrder(CurrentOrder);
  323.   end;
  324. end;
  325.  
  326. procedure TTutorApp.ShowOrder(AOrderNum: Integer);
  327. begin
  328.   CurrentOrder := AOrderNum;
  329.   OrderInfo := POrderObj(OrderColl^.At(CurrentOrder))^.TransferRecord;
  330.   with OrderWindow^ do
  331.   begin
  332.     SetData(OrderInfo);
  333.     Counter^.SetCurrent(CurrentOrder + 1);
  334.   end;
  335.   if CurrentOrder > 0 then EnableCommands([cmOrderPrev])
  336.   else DisableCommands([cmOrderPrev]);
  337.   if OrderColl^.Count > 0 then EnableCommands([cmOrderNext]);
  338.   if CurrentOrder >= OrderColl^.Count - 1 then DisableCommands([cmOrderNext]);
  339.   EnableCommands([cmOrderNew, cmOrderSave]);
  340. end;
  341.  
  342. var
  343.   TutorApp: TTutorApp;
  344.  
  345. begin
  346.   TutorApp.Init;
  347.   TutorApp.Run;
  348.   TutorApp.Done;
  349. end.
  350.